// post-detail.js
const { blogPosts } = require('../../utils/data');
const app = getApp();

Page({
  data: {
    post: null,
    isLiked: false,
    showShareMenu: false,
    showPosterModal: false,
    showCommentModal: false,
    t: {}, // Translations
    currentLanguage: 'en',
    comments: [
      {
        id: 1,
        author: 'Reader123',
        avatar: '/assets/1.png',
        content: 'Great article! I learned a lot from this.',
        date: '2023-07-20',
        likes: 5
      },
      {
        id: 2,
        author: 'BlogFan',
        avatar: '/assets/1.png',
        content: 'I have a question about the third point you made. Could you elaborate more on that?',
        date: '2023-07-19',
        likes: 2
      },
      {
        id: 3,
        author: 'TechEnthusiast',
        avatar: '/assets/1.png',
        content: 'This is exactly what I was looking for. Thanks for sharing!',
        date: '2023-07-18',
        likes: 8
      }
    ]
  },
  
  onLoad: function(options) {
    // Initialize translations
    this.loadLanguage();
    
    const postId = parseInt(options.id);
    // Find the post by ID
    const post = blogPosts.find(p => p.id === postId);
    
    if (post) {
      // Set page title to post title
      wx.setNavigationBarTitle({
        title: post.title
      });
      
      this.setData({
        post
      });
    } else {
      // Post not found, redirect back
      wx.showToast({
        title: 'Post not found',
        icon: 'error'
      });
      setTimeout(() => {
        wx.navigateBack();
      }, 1500);
    }
  },
  
  // Load translations based on current language
  loadLanguage() {
    const i18n = app.globalData.i18n;
    const currentLanguage = i18n.getLanguage();
    
    // Create translations object
    const t = {
      comments: i18n.t('comments'),
      writeComment: i18n.t('writeComment'),
      postComment: i18n.t('postComment'),
      share: i18n.t('share'),
      shareTo: i18n.t('shareTo'),
      generatePoster: i18n.t('generatePoster'),
      savePoster: i18n.t('savePoster'),
      likes: i18n.t('likes')
    };
    
    this.setData({
      t,
      currentLanguage
    });
  },
  
  // Handle language change event from navigation bar
  onLanguageChange(e) {
    const language = e.detail.language;
    app.globalData.i18n.setLanguage(language);
    this.loadLanguage();
  },
  
  onLikeTap() {
    const isLiked = !this.data.isLiked;
    const post = this.data.post;
    
    // Update like count
    post.likes = isLiked ? post.likes + 1 : post.likes - 1;
    
    this.setData({
      isLiked,
      post
    });
    
    wx.showToast({
      title: isLiked ? 'Liked!' : 'Like removed',
      icon: 'success'
    });
  },
  
  onShareTap() {
    this.setData({
      showShareMenu: true
    });
  },
  
  onCloseShareMenu() {
    this.setData({
      showShareMenu: false
    });
  },
  
  onShareToFriend() {
    this.setData({
      showShareMenu: false
    });
    wx.showToast({
      title: 'Shared to friend',
      icon: 'success'
    });
  },
  
  onShareToMoments() {
    this.setData({
      showShareMenu: false
    });
    wx.showToast({
      title: 'Shared to moments',
      icon: 'success'
    });
  },
  
  onGeneratePoster() {
    this.setData({
      showShareMenu: false,
      showPosterModal: true
    });
  },
  
  onClosePosterModal() {
    this.setData({
      showPosterModal: false
    });
  },
  
  onSavePoster() {
    this.setData({
      showPosterModal: false
    });
    wx.showToast({
      title: 'Poster saved to album',
      icon: 'success'
    });
  },
  
  onCommentLike(e) {
    const commentId = e.currentTarget.dataset.id;
    const comments = this.data.comments.map(comment => {
      if (comment.id === commentId) {
        return {
          ...comment,
          likes: comment.likes + 1
        };
      }
      return comment;
    });
    
    this.setData({
      comments
    });
  },
  
  onOpenCommentModal() {
    this.setData({
      showCommentModal: true
    });
  },
  
  onCloseCommentModal() {
    this.setData({
      showCommentModal: false
    });
  },
  
  onSubmitComment(e) {
    const commentContent = e.detail.comment;
    
    const newComment = {
      id: this.data.comments.length + 1,
      author: 'You',
      avatar: '/assets/avatar.png',
      content: commentContent,
      date: new Date().toISOString().split('T')[0],
      likes: 0
    };
    
    this.setData({
      comments: [newComment, ...this.data.comments],
      showCommentModal: false
    });
  }
})